import { createStore } from "vuex";
import { getUserInfoFn } from "@/api/login";
const store = createStore({
    // 各种状态数据
    state(){
        return {
            // 管理员信息
            userInfo: {},
            // 导航菜单
            menu: [],
            // 控制导航菜单是否隐藏
            isCollapse: false
        }
    },
    mutations: {
        setUserInfo(state, userInfo){
            state.userInfo = userInfo
            state.menu = userInfo.menus
        },
        // 修改isCollapse状态
        setCollapse(state){
            state.isCollapse = !state.isCollapse
        }
    },
    actions: {
        // context看做仓库实例
        getUserInfo(context){
            return new Promise((resolve, reject) => {
                getUserInfoFn().then(res=>{
                    context.commit('setUserInfo', res.data)
                    resolve(res)
                }).catch(err=> reject(err))
            })
        }
    }
})

export default store